package com.itheima.Test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        lo:
        while (true) {
            //主界面
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updataStudent(list);
                    break;
                case "4":
                    //System.out.println("查看学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("你的输入有误");
                    break;
            }
        }
    }

    //获取所输入学号对应的对象的索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (sid.equals(list.get(i).getSid())) {
                index = i;
                break;
            }
        }
        return index;
    }

    //添加学生信息
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学号:");
        String sid;
        while (true) {
            sid = sc.next();
            int index = getIndex(list, sid);
            if (index == -1) {
                break;
            } else {
                System.out.println("该学号已被占用,请重新输入:");
            }
        }
        //判断集合长度.若大于一,则需要判断学号是否已被占用
        /*if (list.size() >= 1) {
            lo:
            while (true) {
                for (int i = 0; i < list.size(); i++) {
                    if (sid.equals(list.get(i).getSid())) {
                        System.out.println("学号已被占用,请重新输入学号:");
                        sid = sc.next();//如果学号被占用,重新输入
                        break;
                    } else {
                        //等待输入的学号和每个学生对象的学号比对完,并且都不相同,结束while死循环
                        if (i == (list.size() - 1))
                            break lo;
                    }
                }
            }
        }*/
        System.out.println("请输入姓名:");
        String name = sc.next();
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        System.out.println("请输入生日:");
        String birthday = sc.next();
        Student stu = new Student(sid, name, age, birthday);//创建学生对象录入信息
        list.add(stu);
        System.out.println("信息录入成功");
    }

    //删除学生信息
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        if (list.size() == 0) {
            System.out.println("当前无信息无法进行删除操作");
            return;
        }
        System.out.println("请输入您要删除的学生学号:");
        String sid = sc.next();
        int index = getIndex(list, sid);
        if (index >= 0) {
            list.remove(index);
            System.out.println("删除成功");
        } else {
            System.out.println("您输入的学号不存在");
        }
    }

    //修改学生信息
    private static void updataStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无信息无法进行修改操作");
            return;
        }
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生学号:");
        String sid = sc.next();
        int index = getIndex(list, sid);

        if (index < 0) {
            System.out.println("您输入的学号不存在");
        } else {
            System.out.println("请输入新的学号:");
            String newSid = sc.next();
            lo:
            while (true) {
                for (int i = 0; i < list.size(); i++) {
                    //新的学号可以不变,即仍可以设置为被修改的学生原来的学号
                    if (newSid.equals(list.get(i).getSid()) && !newSid.equals(sid)) {
                        System.out.println("学号已被占用,请重新输入学号:");
                        newSid = sc.next();
                        break;
                    } else if (i == (list.size() - 1))
                        break lo;
                }
            }
            System.out.println("请输入新的姓名:");
            String newName = sc.next();
            System.out.println("请输入新的年龄:");
            int newAge = sc.nextInt();
            System.out.println("请输入新的生日:");
            String newBirthday = sc.next();
            Student stu = new Student(newSid, newName, newAge, newBirthday);
            list.set(index, stu);
            System.out.println("信息修改成功");
        }
    }

    //查看学生信息
    public static void queryStudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("当前无信息,请添加后重新查询");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getSid() + "\t" + list.get(i).getName() + "\t" + list.get(i).getAge() + "\t\t" + list.get(i).getBirthday());
        }
    }
}
